﻿/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using ASN1Decoder.Builders.Visibles;
using ASN1Decoder.Builders.Nodes;
using Core.Model;
using Exceptions;
using Core.Model.Groups;

namespace ASN1Decoder.Builders
{
    internal class ApplicationBuilder
    {
        internal static Application Build(TaggedNode node)
        {
            var app = new Application();

            GroupBuilder.Build(node, app);

            TaggedNode e;

            if (( e = node[ASN1ContextCodes.C_ON_SPAWN_CLOSE_DOWN]) != null)
                app.OnSpawnCloseDown = ActionBuilder.Build(e, app);

            if (( e = node[ASN1ContextCodes.C_ON_RESTART]) != null)
                app.OnRestart = ActionBuilder.Build(e, app);

            SequenceNode defaultAttributes = node[ASN1ContextCodes.C_DEFAULT_ATTRIBUTES];
            BuildDefaultAttributes(defaultAttributes, app);

            return app;
        }

        private static void BuildDefaultAttributes(SequenceNode nodes, Application app)
        {

            foreach (TaggedNode attr in nodes)
            {
                switch (attr.Tag)
                {
                    case (ASN1ContextCodes.C_CHARACTER_SET):
                        app.CharacterSet = attr.First.IntValue;
                        break;

                    //Colours
                    case (ASN1ContextCodes.C_BACKGROUND_COLOUR):
                        app.OriginalBackgroundColour = ColourBuilder.Build(attr.First,app);
                        break;
                    case (ASN1ContextCodes.C_TEXT_COLOUR):
                        app.OriginalTextColour = ColourBuilder.Build(attr.First,app);
                        break;
                    case (ASN1ContextCodes.C_BUTTON_REF_COLOUR):
                        app.ButtonRefColour = ColourBuilder.Build(attr.First,app);
                        break;
                    case (ASN1ContextCodes.C_HIGHLIGHT_REF_COLOUR):
                        app.HighlightRefColour = ColourBuilder.Build(attr.First,app);
                        break;
                    case (ASN1ContextCodes.C_SLIDER_REF_COLOUR):
                        app.SliderRefColour = ColourBuilder.Build(attr.First,app);
                        break;

                    // Content hooks
                    case (ASN1ContextCodes.C_BITMAP_CONTENT_HOOK):
                        app.BitmapContentHook = attr.First.IntValue;
                        break;
                    case (ASN1ContextCodes.C_STREAM_CONTENT_HOOK):
                        app.StreamContentHook = attr.First.IntValue;
                        break;
                    case (ASN1ContextCodes.C_TEXT_CONTENT_HOOK):
                        app.TextContentHook = attr.First.IntValue;
                        break;
                    case (ASN1ContextCodes.C_LINE_ART_CONTENT_HOOK):
                        app.LineArtContentHook = attr.First.IntValue;
                        //throw new Exception( "1.06 does not allow content hook change" );
                        break;

                    //fonts
                    case (ASN1ContextCodes.C_FONT_ATTRIBUTES):
                        app.OriginalFontAttributes = TextBuilder.FontAttributesBuilder((OctetStringNode)attr.First);
                        break;

                    case (ASN1ContextCodes.C_FONT):
                    case (ASN1ContextCodes.C_FONT2):

                        break;

                    default:
                        throw new OpenMhegException("Unknown Default Attribute");
                        

                }

            }

        }
    }
}
